package com.qfedu.edu.seckill.config;

import com.qfedu.edu.seckill.filter.AuthenticationFilter;
import com.qfedu.edu.seckill.filter.LoginFilter;
import com.qfedu.edu.seckill.manager.RedisManager;
import org.qfedu.edu.seckill.service.IJwtService;
import org.qfedu.edu.seckill.service.ITokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutHandler;

/**
 * SpringSecurity这个框架的配置文件
 */
@SpringBootConfiguration
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true) //使能Security中的注解鉴权
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private LogoutHandler logoutHandler;

    @Autowired
    private ITokenService tokenService;

    @Autowired
    private RedisManager redisManager;

    @Autowired
    private IJwtService jwtService;


    /**
     * 配置UserDetailService这些
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint) //设置没有权限访问的时候要执行的处理类
                .and().csrf().disable()   //关闭防止跨站点伪造
                .authorizeRequests().anyRequest().authenticated() //任何请求登录之后才能访问
                .and().logout().logoutUrl("/users/logout") //设置退出功能的地址是啥
                .addLogoutHandler(logoutHandler) //设置退出功能的处理类是啥
                .and()
                .addFilter(new LoginFilter(authenticationManager(),tokenService,redisManager))  //设置的登录过滤器
                .addFilter(new AuthenticationFilter(authenticationManager(),tokenService,redisManager,jwtService)) //设置的是认证的过滤器
                .httpBasic();

    }


    /**
     * 这个主要配置的是哪些地址不拦截就可以访问
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        //设置部分地址不需要登陆就能访问
        web.ignoring().antMatchers("/searchservice/search/**");

        //放行所有的swagger的请求地址
        //下面需要将 swagger的请求的地址放行 (不需要登陆就能直接访问)
        //在这里需要将swagger的所有的请求路径全部放行
        //所需要用到的静态资源，允许访问
        web.ignoring().antMatchers( "/swagger-ui.html",
                "/swagger-ui/*",
                "/swagger-resources/**",
                "/v2/api-docs",
                "/v3/api-docs",
                "/webjars/**");


        web.ignoring().antMatchers("/orderservice/order/createOrder");
        web.ignoring().antMatchers("/payservice/pays/weixinPayNotify");
    }
}
